package com.zx.deploy.common.auth;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.zx.deploy.common.annotation.NotAuthPassport;



/**
 * 
* @Description: 会话过滤
* @author jbyu 
* @date 2018年1月23日 上午10:28:21 
*
 */
public class AuthInterceptor extends HandlerInterceptorAdapter {
	
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Object loginInfo = request.getSession().getAttribute("userLogin");
    	
        if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {
            NotAuthPassport notAuthPassport = ((HandlerMethod) handler).getMethodAnnotation(NotAuthPassport.class);
            //声明不验证权限
            if (notAuthPassport != null)
                return true;
            else {
                if (loginInfo == null) {
                    response.getWriter().write("<script>location.href='" + request.getContextPath() + "/login'</script>");
                    return false;
                }
            }
           
        }
        return true;
    }
    
    
}